//
// Created by dgy on 2022/11/11.
//

#include "server.h"
#include "player.h"

Battlefield PlayerB_BF;
char * PlayerBName = "xxg";

// 玩家部署舰艇
int DeploymentB()
{
    for(int i=0;i<M;i++)
        for(int j=0;j<M;j++)
        {
            PlayerB_BF.area[i][j] = 1;
        }
    // add your arrangement, for example:
    PlayerB_BF.area[0][0] = D; PlayerB_BF.area[0][1] = D;
    PlayerB_BF.area[1][0] = S; PlayerB_BF.area[1][1] = S; PlayerB_BF.area[1][2] = S;
    PlayerB_BF.area[2][0] = C; PlayerB_BF.area[2][1] = C; PlayerB_BF.area[2][2] = C;
    PlayerB_BF.area[3][6] = B; PlayerB_BF.area[3][7] = B; PlayerB_BF.area[3][8] = B; PlayerB_BF.area[3][9] = B;
    PlayerB_BF.area[4][5] = A; PlayerB_BF.area[4][6] = A; PlayerB_BF.area[4][7] = A; PlayerB_BF.area[4][8] = A; PlayerB_BF.area[4][9] = A;

    printBF(&PlayerB_BF);
    return 0;
}

static int lasti = 0;
static int lastj = 0;
static int step=0;
static int checkMaritx[10][10]={0};
// 根据上次是否击中确定策略，选择下一个攻击点
// 可以自己创建数据结构记录之前的击中与否信息
int StrategyB()
{
    int i, j;
    // 从服务器端获取上次攻击是否击中信息
    int isHit = BLastHitA();
    i = lasti;
    j = lastj;
    //未击中的情况
    if (isHit == 0) {
        i=lasti;
        if (j<M-1)
            j=lastj+1;
        while (checkMaritx[i][j]==1){
            if (j>0&&checkMaritx[i][j-1]==1)
                j=lastj-1;
            else if (i>0&&checkMaritx[i-1][j]==1)
                i=lasti-1;
            else if (i<M-1&&checkMaritx[i+1][j])
                i=lasti+1;
            step++;
            if (step>4){
                i=rand()%M;
                j=rand()%M;
            }
        }
    } else {
        /*
         * 击中后，有三种情况需要处理
         * 1.在中间区域，要找他周围的四个位置
         * 2.在四个角上，要找他周围的两个位置
         * 3.在边缘，要找他周围的三个位置
         * */
        step=0;
        if (i >= 1 && i < M - 1 && j >= 1 && j < M - 1) {
            if (checkMaritx[i + 1][j] == 0)
                i = lasti + 1;
            else if(checkMaritx[i-1][j]==0)
                i = lasti - 1;
            else if (checkMaritx[i][j + 1] == 0)
                j = lastj + 1;
            else if (checkMaritx[i][j - 1] == 0)
                j = lastj - 1;
        } else if ((i == 0 && j == 0) || (i == 0 && j == M - 1) || (i == M - 1 && j == 0) ||
                   (i == M - 1 && j == M - 1)) {
            if (i == 0 && j == 0) {
                if (checkMaritx[i][j + 1] == 0)
                    j = lastj + 1;
                else if (checkMaritx[i + 1][j] == 0)
                    i = lasti + 1;
            } else if (i == 0 && j == M - 1) {
                if (checkMaritx[i][j - 1] == 0)
                    j = lastj - 1;
                else if (checkMaritx[i + 1][j] == 0)
                    i = lasti + 1;
            } else if (i == M - 1 && j == 0) {
                if (checkMaritx[i][j + 1] == 0)
                    j = lastj + 1;
                else if (checkMaritx[i - 1][j] == 0)
                    i = lasti - 1;
            } else if (i == M - 1 && j == M - 1) {
                if (checkMaritx[i][j - 1] == 0)
                    j = lastj - 1;
                else if (checkMaritx[i - 1][j] == 0)
                    i = lasti - 1;
            }
        } else {
            if (i == 0 || i == M - 1) {
                if (checkMaritx[i][j + 1] == 0)
                    j = lastj + 1;
                else if (checkMaritx[i][j - 1] == 0)
                    j = lastj - 1;
                else if (i == 0 && checkMaritx[i + 1][j] == 0)
                    i = lasti + 1;
                else if (i == M - 1 && checkMaritx[i - 1][j] == 0)
                    i = lasti - 1;
            } else if (j == 0 || j == M - 1) {
                if (checkMaritx[i - 1][j] == 0)
                    i = lasti - 1;
                else if (checkMaritx[i + 1][j] == 0)
                    i = lasti + 1;
                else if (j == 0 && checkMaritx[i][j + 1] == 0)
                    j = lastj + 1;
                else if (j == M - 1 && checkMaritx[i][j - 1] == 0)
                    j = lastj - 1;
            }
        }
    }
    //记录上一次的情况
    lasti = i;
    lastj = j;
    checkMaritx[i][j] = 1;
    step++;
    firetoA(i, j);
    return 0;
}
